﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ContinuousMacro.CApCMContinuousMacroDisplayManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Linq;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ContinuousMacro;

internal class CApCMContinuousMacroDisplayManager
{
  private CApCMDisplayContinuousMacrosCmd parentCommand;
  private List<CApCMGraphicsShapeDisplayer<CApCMMacroShape>> macroList = new List<CApCMGraphicsShapeDisplayer<CApCMMacroShape>>();
  private bool isDisplaying;

  private TxComponent DisplayParent { get; set; }

  private bool IsDisplaying
  {
    get => this.isDisplaying;
    set
    {
      this.isDisplaying = value;
      this.parentCommand.IsPushed = this.isDisplaying;
    }
  }

  public CApCMContinuousMacroDisplayManager(CApCMDisplayContinuousMacrosCmd parentCommand)
  {
    this.parentCommand = parentCommand;
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
  }

  public void Toggle(object cmdParams)
  {
    if (cmdParams == null)
    {
      if (this.IsDisplaying)
        this.UnDisplayAllMacros();
      else
        this.DisplayMacros(this.GetViaLocations((IList<ITxObject>) TxApplication.ActiveSelection.GetItems()));
    }
    else
    {
      IList<TxRoboticViaLocationOperation> viaLocations;
      switch (this.ParseParameters(cmdParams, out viaLocations))
      {
        case CApCMContinuousMacroDisplayManager.OperatingMode.Add:
          this.DisplayMacros(viaLocations);
          break;
        case CApCMContinuousMacroDisplayManager.OperatingMode.Remove:
          this.UnDisplayMacros(viaLocations);
          break;
        case CApCMContinuousMacroDisplayManager.OperatingMode.Get:
          using (List<CApCMGraphicsShapeDisplayer<CApCMMacroShape>>.Enumerator enumerator = this.macroList.GetEnumerator())
          {
            while (enumerator.MoveNext())
            {
              CApCMGraphicsShapeDisplayer<CApCMMacroShape> current = enumerator.Current;
              viaLocations.Add(current.ViaLocation);
            }
            break;
          }
      }
    }
  }

  private CApCMContinuousMacroDisplayManager.OperatingMode ParseParameters(
    object cmdParams,
    out IList<TxRoboticViaLocationOperation> viaLocations)
  {
    viaLocations = (IList<TxRoboticViaLocationOperation>) null;
    if (!(cmdParams is IList<object> objectList) || objectList.Count < 1 || objectList.Count > 2)
      return CApCMContinuousMacroDisplayManager.OperatingMode.None;
    CApCMContinuousMacroDisplayManager.OperatingMode parameters;
    if (objectList.Count == 2)
    {
      if (!(objectList[1] is bool))
        return CApCMContinuousMacroDisplayManager.OperatingMode.None;
      parameters = !(bool) objectList[1] ? CApCMContinuousMacroDisplayManager.OperatingMode.Remove : CApCMContinuousMacroDisplayManager.OperatingMode.Add;
    }
    else
      parameters = CApCMContinuousMacroDisplayManager.OperatingMode.Get;
    viaLocations = objectList[0] as IList<TxRoboticViaLocationOperation>;
    return parameters;
  }

  public void Delete()
  {
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
    this.UnDisplayAllMacros();
  }

  private IList<TxRoboticViaLocationOperation> GetViaLocations(IList<ITxObject> objectList)
  {
    List<TxRoboticViaLocationOperation> viaLocations = new List<TxRoboticViaLocationOperation>();
    if (objectList == null)
      return (IList<TxRoboticViaLocationOperation>) viaLocations;
    foreach (ITxObject itxObject in (IEnumerable<ITxObject>) objectList)
    {
      TxRoboticViaLocationOperation locationOperation = itxObject as TxRoboticViaLocationOperation;
      if (TxRoboticViaLocationOperation.op_Inequality(locationOperation, (ITxObject) null) && locationOperation.ParentRoboticOperation is ITxContinuousOperation)
        viaLocations.Add(locationOperation);
    }
    return (IList<TxRoboticViaLocationOperation>) viaLocations;
  }

  private void DisplayMacros(IList<TxRoboticViaLocationOperation> viaLocations)
  {
    if (TxComponent.op_Equality(this.DisplayParent, (ITxObject) null))
      this.DisplayParent = CApCMContinuousMacroUtilities.CreateDisplayParent();
    foreach (TxRoboticViaLocationOperation viaLocation1 in (IEnumerable<TxRoboticViaLocationOperation>) viaLocations)
    {
      TxRoboticViaLocationOperation viaLocation = viaLocation1;
      CApCMMacroShape macroShape = CApCMMacroShape.Create(viaLocation);
      if (macroShape != null && !this.macroList.Any<CApCMGraphicsShapeDisplayer<CApCMMacroShape>>((Func<CApCMGraphicsShapeDisplayer<CApCMMacroShape>, bool>) (x => TxRoboticViaLocationOperation.op_Equality(x.ViaLocation, (ITxObject) viaLocation))))
      {
        CApCMContinuousMacroDisplayer continuousMacroDisplayer = new CApCMContinuousMacroDisplayer(viaLocation, this.DisplayParent, macroShape);
        this.macroList.Add((CApCMGraphicsShapeDisplayer<CApCMMacroShape>) continuousMacroDisplayer);
        continuousMacroDisplayer.Deleted += new Action<CApCMGraphicsShapeDisplayer<CApCMMacroShape>>(this.RemoveDeletedMacroDisplayer);
      }
    }
    if (this.macroList.Count <= 0)
      return;
    this.IsDisplaying = true;
  }

  private void UnDisplayMacros(IList<TxRoboticViaLocationOperation> viaLocations)
  {
    foreach (TxRoboticViaLocationOperation viaLocation1 in (IEnumerable<TxRoboticViaLocationOperation>) viaLocations)
    {
      TxRoboticViaLocationOperation viaLocation = viaLocation1;
      CApCMGraphicsShapeDisplayer<CApCMMacroShape> graphicsShapeDisplayer = this.macroList.Find((Predicate<CApCMGraphicsShapeDisplayer<CApCMMacroShape>>) (x => TxRoboticViaLocationOperation.op_Equality(x.ViaLocation, (ITxObject) viaLocation)));
      if (graphicsShapeDisplayer != null)
      {
        graphicsShapeDisplayer.Deleted -= new Action<CApCMGraphicsShapeDisplayer<CApCMMacroShape>>(this.RemoveDeletedMacroDisplayer);
        graphicsShapeDisplayer.Delete();
        this.macroList.Remove(graphicsShapeDisplayer);
      }
    }
    if (this.macroList.Count != 0)
      return;
    if (TxComponent.op_Inequality(this.DisplayParent, (ITxObject) null))
    {
      ((TxBaseGeometryCreationObject) this.DisplayParent).Delete();
      this.DisplayParent = (TxComponent) null;
    }
    this.IsDisplaying = false;
  }

  private void UnDisplayAllMacros()
  {
    foreach (CApCMGraphicsShapeDisplayer<CApCMMacroShape> macro in this.macroList)
    {
      macro.Deleted -= new Action<CApCMGraphicsShapeDisplayer<CApCMMacroShape>>(this.RemoveDeletedMacroDisplayer);
      macro.Delete();
    }
    this.macroList.Clear();
    if (TxComponent.op_Inequality(this.DisplayParent, (ITxObject) null))
    {
      ((TxBaseGeometryCreationObject) this.DisplayParent).Delete();
      this.DisplayParent = (TxComponent) null;
    }
    this.IsDisplaying = false;
  }

  private void RemoveDeletedMacroDisplayer(
    CApCMGraphicsShapeDisplayer<CApCMMacroShape> macroDisplayer)
  {
    macroDisplayer.Deleted -= new Action<CApCMGraphicsShapeDisplayer<CApCMMacroShape>>(this.RemoveDeletedMacroDisplayer);
    this.macroList.Remove(macroDisplayer);
    if (this.macroList.Count != 0)
      return;
    this.UnDisplayAllMacros();
  }

  private void ActiveDocument_Unloading(object sender, TxDocument_UnloadingEventArgs args)
  {
    this.UnDisplayAllMacros();
  }

  private enum OperatingMode
  {
    Add,
    Remove,
    Get,
    None,
  }
}
